package com.cn.jsdr.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class MySql {
        public static final String KEY_WORD = "keyword";
        public static final String KEY_WORD_Q = "select";
        public static final String KEY_WORD_A = "insert";
        public static final String KEY_WORD_U = "update";
        public static final String KEY_WORD_D = "delete";
        public static final String M_TABLE = "mtable";
        public static final String STATIC_COND = "statecond";
        public static final String DYN_CONDS = "dynconds";
        public static final String ENDING = "ending";
        public static final String JOINON = "joinon";
        public static final String SORT = "sort";
        public static final String PAGING = "paging";
        public static final String PAGE_SIZE = "pagesize";
        public static final String PAGE_INDEX = "pageindex";
        public static final String PRIFLAG = "1";
        public static final String NOTNULL = "4";
        public static final String NUMFLAG = "2";
        public static final String BIGSPLIT = "@";
        public static final String MINSPLIT = "#";


        public static String getDeletesql(Map<String, String> sqlslice, Map<String, Object> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("delete")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String statecond = (String)sqlslice.get("statecond");
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    StringBuffer sb = new StringBuffer();
                    statecond = statecond == null ? "" : statecond;
                    ending = ending == null ? "" : ending;
                    dynconds = dynconds != null && dynconds != "" ? getCond(dynconds, param) : "";
                    sb.append(keyword).append(" ").append(mtable).append(" ").append(statecond).append(" ").append(dynconds).append(" ").append(ending);
                    return "" + sb;
                } else {
                    throw new RuntimeException("mtable :要删除的表不能为空！" + mtable);
                }
            }
        }

        public static String getQuerySql(Map<String, String> sqlslice, Map<String, Object> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("select")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String joinon = (String)sqlslice.get("joinon");
                    String statecond = (String)sqlslice.get("statecond");
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    StringBuffer sb = new StringBuffer();
                    statecond = statecond == null ? "" : statecond;
                    ending = ending == null ? "" : ending;
                    String sort = param.get("sort") == null ? "" : "" + param.get("sort");
                    String paging = (String)sqlslice.get("paging");
                    paging = paging == null ? "" : getPaging(paging, param);
                    dynconds = dynconds != null && dynconds != "" ? getCond(dynconds, param) : "";
                    joinon = joinon != null && joinon != "" ? getCond(joinon, param) : "";
                    sb.append(keyword).append(" ").append(mtable).append(" ").append(joinon).append(" ").append(statecond).append(" ").append(dynconds).append(" ").append(sort).append(" ").append(paging).append(" ").append(ending);
                    return sb.toString();
                } else {
                    throw new RuntimeException("mtable :要查询的表不能为空！" + mtable);
                }
            }
        }

        public static String getUpdateSql(Map<String, String> sqlslice, Map<String, Object> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("update")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String joinon = (String)sqlslice.get("joinon");
                    String statecond = (String)sqlslice.get("statecond");
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    StringBuffer sb = new StringBuffer();
                    statecond = statecond == null ? "" : statecond;
                    ending = ending == null ? "" : ending;
                    dynconds = dynconds != null && dynconds != "" ? getCond(dynconds, param) : "";
                    joinon = joinon == "" ? "" : getCond(joinon, param);
                    sb.append(keyword).append(" ").append(mtable).append(" ").append(joinon).append(" ").append(statecond).append(" ").append(dynconds).append(" ").append(ending);
                    return "" + sb;
                } else {
                    throw new RuntimeException("mtable :要更新的表不能为空！" + mtable);
                }
            }
        }

        public static String getAddSql(Map<String, String> sqlslice, Map<String, Object> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("insert")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    ending = ending == null ? "" : ending;
                    StringBuffer sb = new StringBuffer();
                    dynconds = dynconds != null && dynconds != "" ? getOneInsert(dynconds, param) : "";
                    sb.append(keyword).append(" ").append(mtable).append(" ").append(dynconds).append(" ").append(ending);
                    return "" + sb;
                } else {
                    throw new RuntimeException("mtable :要更新的表不能为空！" + mtable);
                }
            }
        }

        public static String getAddSqls(Map<String, String> sqlslice, List<Map<String, Object>> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("insert")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    ending = ending == null ? "" : ending;
                    StringBuffer sb = new StringBuffer();
                    dynconds = dynconds != null && dynconds != "" ? getInserts(dynconds, param) : "";
                    sb.append(keyword).append(" ").append(mtable).append(" ").append(dynconds).append(" ").append(ending);
                    return "" + sb;
                } else {
                    throw new RuntimeException("mtable :要更新的表不能为空！" + mtable);
                }
            }
        }

        public static String[] getBatchSqls(Map<String, String> sqlslice, List<Map<String, Object>> param) {
            String keyword = (String)sqlslice.get("keyword");
            if (!keyword.equalsIgnoreCase("insert")) {
                throw new RuntimeException("keyword :关键字错误！" + keyword);
            } else {
                String mtable = (String)sqlslice.get("mtable");
                if (mtable != null && !"".equals(mtable)) {
                    String dynconds = (String)sqlslice.get("dynconds");
                    String ending = (String)sqlslice.get("ending");
                    ending = ending == null ? "" : ending;
                    StringBuffer sb = new StringBuffer();
                    dynconds = dynconds != null && dynconds != "" ? dynconds : "";
                    sb.append(keyword).append(" ").append(mtable).append(" ");
                    return batchInserts(sb.toString(), dynconds, param);
                } else {
                    throw new RuntimeException("mtable :要更新的表不能为空！" + mtable);
                }
            }
        }

        public static String getOrder(String sort, Map<String, Object> m) {
            String[] condArray = mySplit(sort, "#");
            String sor = "" + m.get(condArray[0]);
            return sor != null && !"".equals(sor) ? condArray[0] + " " + sor : condArray[0];
        }

        public static String getPaging(String paging, Map<String, Object> m) {
            String[] condArray = mySplit(paging, "#");
            String pages = condArray[0];
            if (pages != null && !"".equals(pages.trim())) {
                Object pagesize = m.get("pagesize");
                if (pagesize != null && !"".equals(pagesize)) {
                    int ps = (Integer)pagesize;
                    Object pageindex = m.get("pagesize");
                    if (pageindex != null && !"".equals(pageindex)) {
                        int pi = (Integer)pageindex;
                        int mynum = (pi - 1) * ps;
                        return pages + " " + ps + " " + condArray[1] + " " + mynum;
                    } else {
                        return pages + " " + ps;
                    }
                } else {
                    return "";
                }
            } else {
                return "";
            }
        }

        public static String[] mySplit(String text, String separat) {
            int off = 0;
            int len = 0;
            ArrayList<String> list = new ArrayList();
            boolean var5 = false;

            int next;
            do {
                ++len;
                next = text.indexOf(separat, off);
                if (next == -1) {
                    list.add(text.substring(off));
                    String[] result = new String[len];
                    return (String[])list.toArray(result);
                }

                list.add(text.substring(off, next));
                off = next + 1;
            } while(next != -1);

            return null;
        }

        public static String joint(Object v, String[] spl, Boolean sqltyp, Boolean skip) {
            StringBuffer sb = new StringBuffer();
            String spl3 = spl[3];
            if (v != null && !"".equals(v)) {
                if ("2".equals(spl3)) {
                    if (!(v instanceof Integer) && !(v instanceof Boolean) && !(v instanceof Float)) {
                        throw new RuntimeException("sql字段‘" + Arrays.toString(spl) + "’类型定义错误！值为字符串型！");
                    } else {
                        return sqltyp ? sb.append(v) + " " : sb.append(spl[1]).append(v) + " ";
                    }
                } else if (v.toString().indexOf("or ", 0) != -1) {
                    throw new RuntimeException("参数【" + v + "】存在违法字符 or！");
                } else {
                    return sqltyp ? sb.append("'").append(v) + "' " : sb.append(spl[1]).append(v).append(spl[2]) + " ";
                }
            } else if (sqltyp && "1".equals(spl3)) {
                return sb.append("'").append(UUID.randomUUID()) + "'";
            } else if ("4".equals(spl3)) {
                throw new RuntimeException("参数‘" + spl[0] + "’是必填项！");
            } else {
                return skip ? null : "";
            }
        }

        public static String getCond(String cond, Map<String, Object> m) {
            if (cond != null && cond != "") {
                String[] condArray = mySplit(cond, "@");
                StringBuffer sb = new StringBuffer();

                for(int i = 0; i < condArray.length; ++i) {
                    String[] condA = mySplit(condArray[i], "#");
                    if (condA.length == 1) {
                        sb.append(condA[0]);
                    } else {
                        if (condA.length < 4) {
                            throw new RuntimeException("参数‘" + Arrays.toString(condA) + "’长度不够！");
                        }

                        Object v = m.get(condA[0]);
                        String spll = joint(v, condA, false, false);
                        sb.append(spll);
                    }
                }

                return "" + sb;
            } else {
                return "";
            }
        }

        public static String getOneInsert(String cond, Map<String, Object> m) {
            String[] condArray = mySplit(cond, "@");
            StringBuffer sbKey = new StringBuffer();
            StringBuffer sbVal = new StringBuffer();
            Boolean flag = true;

            for(int i = 0; i < condArray.length; ++i) {
                String[] condA = mySplit(condArray[i], "#");
                Object val = m.get(condA[0].trim());
                if (condA.length < 4) {
                    throw new RuntimeException("参数‘" + condArray[i] + "’长度不够！");
                }

                String spll = joint(val, condA, true, false);
                if (!"".equals(spll)) {
                    if (flag) {
                        flag = false;
                    } else {
                        sbKey.append(",");
                        sbVal.append(",");
                    }

                    sbVal.append(spll);
                    sbKey.append(condA[1]).append(" ");
                }
            }

            return "(" + sbKey + ")  VALUES (" + sbVal + ")";
        }

        public static String getInserts(String cond, List<Map<String, Object>> map) {
            String[] condArray = mySplit(cond, "@");
            StringBuffer sbKey = new StringBuffer();
            StringBuffer sbVal = new StringBuffer();
            boolean flag = false;
            Iterator var7 = map.iterator();

            while(true) {
                while(var7.hasNext()) {
                    Map<String, Object> m = (Map)var7.next();
                    boolean isFirst = true;
                    int i;
                    String[] condA;
                    Object val;
                    String spll;
                    if (flag) {
                        sbVal.append(",(");

                        for(i = 0; i < condArray.length; ++i) {
                            condA = mySplit(condArray[i], "#");
                            val = m.get(condA[0].trim());
                            if (condA.length < 4) {
                                throw new RuntimeException("参数‘" + condArray[i] + "’长度不够！");
                            }

                            spll = joint(val, condA, true, true);
                            if (!"".equals(spll)) {
                                if (isFirst) {
                                    isFirst = false;
                                } else {
                                    sbVal.append(",");
                                }

                                sbVal.append(spll);
                            }
                        }

                        sbVal.append(")");
                    } else {
                        sbVal.append("(");
                        sbKey.append("(");
                        flag = true;

                        for(i = 0; i < condArray.length; ++i) {
                            condA = mySplit(condArray[i], "#");
                            val = m.get(condA[0].trim());
                            if (condA.length < 4) {
                                throw new RuntimeException("参数‘" + condArray[i] + "’长度不够！");
                            }

                            spll = joint(val, condA, true, true);
                            if (!"".equals(spll)) {
                                if (isFirst) {
                                    isFirst = false;
                                } else {
                                    sbKey.append(",");
                                    sbVal.append(",");
                                }
                                sbVal.append(spll);
                                sbKey.append(condA[1]).append(" ");
                            }
                        }
                        sbVal.append(")");
                        sbKey.append(")");
                    }
                }

                return sbKey + " VALUES " + sbVal + ";";
            }
        }

        public static String[] batchInserts(String str, String cond, List<Map<String, Object>> map) {
            ArrayList al = new ArrayList();
            int num = 0;
            List<Map<String, Object>> newmap = new ArrayList();
            Iterator var7 = map.iterator();

            while(var7.hasNext()) {
                Map<String, Object> m = (Map)var7.next();
                ++num;
                newmap.add(m);
                if (num % 2 == 0) {
                    al.add(str + getInserts(cond, newmap));
                    newmap.clear();
                }
            }
            al.add(str + getInserts(cond, newmap));
            String[] s = new String[al.size()];
            return (String[])al.toArray(s);
        }

}
